//搜索功能
$(function () {
    //此处获取搜索
    //获取页面上桥的id
    var URL = document.location.toString();
    var arrUrl = URL.split("/");
    bridgeId = arrUrl[arrUrl.length-3] ;
    $("#analysis").click(function () {
        var starttime = $("#startime").val();
        var endtime = $("#endtime").val();
        //跳转到分析页面
        window.location.href="/bridge/getallreport/"+bridgeId+"/"+starttime+"/"+endtime;
    })
})
$(function () {
    //显示饼状图
    var dom = document.getElementById("SenorPieChart");
    var myChart = echarts.init(dom);
    var app = {};
    option = null;
    //先获取浏览器地址栏的数据
    var URL = document.location.toString();
    var arrUrl = URL.split("/");
    var bridgeId = arrUrl[arrUrl.length-3] ;
    var starttime = arrUrl[arrUrl.length-2];
    var endtime = arrUrl[arrUrl.length-1];
    var totalSum = 0;
    var warnigSum = 0;
    var firstWarningSum = 0;
    var secondWarningSum = 0;
    //ajax同步请求数据
    $.ajax(
        {
            url:"/bridge/getallreport",
            type:"POST",
            dataType:"json",
            async:false,
            data:{BID:bridgeId,StartTime:starttime,EndTime:endtime},
            success:function (result) {
                $.each(result,function (i,item) {
                    totalSum += item["dataTotalSum"];
                    warnigSum += item["warningSum"];
                    firstWarningSum += item["firstWarningSum"];
                    secondWarningSum += item["secondWarningSum"];
                });
            },
            error: function (xhr, status, p3, p4) {
                var err = "Error " + " " + status + " " + p3;
                if (xhr.responseText && xhr.responseText[0] == "{")
                    err = JSON.parse(xhr.responseText).message;
                console.log(err);
            }
        });
    var option = {
        series: {
            type: 'sunburst',
            data: [{
                name: '正常数据'+ (totalSum - warnigSum),
                value: totalSum - warnigSum,
                itemStyle: {
                    color: '#CDCDB4'
                }
            }, {
                name: '异常数据 '+ warnigSum,
                value: warnigSum ,
                itemStyle: {
                    color: '#FF8C00'
                },
                children: [{
                    name: '一级预警:'+ firstWarningSum,
                    value: firstWarningSum,
                    itemStyle: {
                        color: '#FF7F00'
                    }
                }, {
                    name: '二级预警:'+ secondWarningSum,
                    value: secondWarningSum,
                    itemStyle: {
                        color: 'red'
                    }
                }]
            }]
        }
    };
    ;
    if (option && typeof option === "object") {
        myChart.setOption(option, true);
    }
})
$(function () {
    //先获取浏览器地址栏的数据
    var URL = document.location.toString();
    var arrUrl = URL.split("/");
    var bridgeId = arrUrl[arrUrl.length-3] ;
    var starttime = arrUrl[arrUrl.length-2];
    var endtime = arrUrl[arrUrl.length-1];
    //显示异常数据统计
    var dom = document.getElementById("showWarning");
    var myChart = echarts.init(dom);
    var app = {};
    option = null;
    app.title = '堆叠条形图';
    //初始化数据值
    var date = [];
    var firstwarningdata = [];
    var secondwarningdata = [];
    var indexFirstWarning = [];//用日期对firstWarning进行索引
    var indexSecondWarning = [];//用日期对secondWarning进行索引
    //ajax请求
    $.ajax(
        {
            url:"/bridge//getreportdetail",
            type:"POST",
            dataType:"json",
            async:false,
            data:{BID:bridgeId,StartTime:starttime,EndTime:endtime},
            success:function (result) {
                $.each(result,function (i,item) {
                    var tmpdate = echarts.format.formatTime('yyyy-MM-dd',+echarts.number.parseDate(item["reportDate"]));
                    date.push(tmpdate);
                    firstwarningdata.push(item["firstWarningSum"]);
                    secondwarningdata.push(item["secondWarningSum"])
                    indexFirstWarning[tmpdate] = item["firstWarningID"];
                    indexSecondWarning[tmpdate] = item["secondWarningID"];
                });
            },
            error: function (xhr, status, p3, p4) {
                var err = "Error " + " " + status + " " + p3;
                if (xhr.responseText && xhr.responseText[0] == "{")
                    err = JSON.parse(xhr.responseText).message;
                console.log(err);
            }
        });
    //触发点击事件，查看异常数据值
    myChart.on('click',function (params) {
        console.log(params);
        //通过两个函数进行输出，传入params.name，为id的拼接
        if (params.componentIndex == 0)
        {
            showFirstWarning(indexFirstWarning[params.name]);
        }else{
            showSecondWarning(indexSecondWarning[params.name]);
        }
    })
    option = {
        tooltip : {
            trigger: 'axis',
            axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        legend: {
            data: ['一级预警数量', '二级预警数量']
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis:  {
            type: 'value'
        },
        yAxis: {
            type: 'category',
            data: date
        },
        series: [
            {
                name: '一级预警数量',
                type: 'bar',
                stack: '总量',
                label: {
                    normal: {
                        show: true,
                        position: 'insideRight'
                    }
                },
                data: firstwarningdata
            },
            {
                name: '二级预警数量',
                type: 'bar',
                stack: '总量',
                label: {
                    normal: {
                        show: true,
                        position: 'insideRight'
                    }
                },
                data: secondwarningdata
            }
        ]
    };;
    if (option && typeof option === "object") {
        myChart.setOption(option, true);
    }
})
function showFirstWarning(WarningID) {
    //对数据进行分解
    console.log(WarningID);
    //让页面显示
    var $body = $('body');
    var div = "<div class='modal-backdrop show'></div>";
    $body.append(div);
    $("#ShowData").css("display","block");
    $("#ShowData").css("padding-right","17px");
    $("#ShowData").addClass("show");
    $("#closeModal").click(function () {
        //关闭div
        $("#ShowData").css("display","none");
        $(".modal-backdrop").removeAttr("class","show");
    })
    //ajax请求
    $.ajax(
        {
            url: "/bridge/getmultidata",
            type: "POST",
            dataType: "json",
            data: {DataID: WarningID},
            success: function (result) {
                ReadWarningData(result);
            }
        });
}
function showSecondWarning(WarningID) {
    //
    console.log(WarningID);

    var $body = $('body');
    var div = "<div class='modal-backdrop show'></div>";
    $body.append(div);
    $("#ShowData").css("display","block");
    $("#ShowData").css("padding-right","17px");
    $("#ShowData").addClass("show");
    $("#closeModal").click(function () {
        //关闭div
        $("#ShowData").css("display","none");
        $(".modal-backdrop").removeAttr("class","show");
    })
    //ajax请求
    $.ajax(
        {
            url: "/bridge/getmultidata",
            type: "POST",
            dataType: "json",
            data: {DataID: WarningID},
            success: function (result) {
                ReadWarningData(result);
            }
        });
}
function ReadWarningData(JsonResult) {
    var warningDataHTml = "";//初始化字符串
    $.each(JsonResult,function (i,item) {
        //遍历读取数据并写入html,拼接html页面
        warningDataHTml += "<tr><td class='text-md-center'>" + item["id"] + "</td><td class='text-md-center'>" + item["paraValue"] + "</td><td class='text-md-center'>" + item["paraTypeCode"] + "</td><td class='text-md-center'>" + item["reachFirst"] + "</td><td class='text-md-center'>" + item["reachSecond"] + "</td><td class='text-md-center'>" + item["systemTime"] + "</td></tr>";
        //console.log(item["paraValue"]);
    });
    //添加字符串
    $("#modalWarningData").html(warningDataHTml);
}